package com.wxtf.platform.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.ConstantInfoHelper;

/**
 * @author 黄威
 * 
 * **/
public class SystemControlServlet extends HttpServlet{

	private static final long serialVersionUID = 8334788852269023388L;
	static Log logger = LogFactory.getLog(SystemControlServlet.class);

	private String logicData = null;
	private String extendData = null;

	private int page = 0;
	private int pagesize = 50;
	
	private String methodName = null;
	private String className = null;
	private String charset = "UTF-8";
	private String contentType = "text/html";

	public void init(ServletConfig cfg) throws ServletException {

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Expires", "-1");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Content-Type", "\"" + contentType + "; charset=" + charset + "\"");
		request.setCharacterEncoding(charset);
		PrintWriter out = response.getWriter();
		StringBuffer sb = new StringBuffer();

		className = request.getParameter(ConstantInfoHelper.KEY_CLASSNAME);
		methodName = request.getParameter(ConstantInfoHelper.KEY_METHODNAME);
		logicData = request.getParameter(ConstantInfoHelper.KEY_LOGICDATA);
		extendData = request.getParameter(ConstantInfoHelper.KEY_EXTENDDATA);

		String p1 = request.getParameter("page");
		String p2 = request.getParameter("pagesize");
		page = StringUtils.isBlank(p1) ? 0 : Integer.valueOf(p1);
		pagesize = StringUtils.isBlank(p2) ? 50 : Integer.valueOf(p2);
		
		try {
			Class<?> ct = Class.forName(className);
			Constructor<?> c = ct.getDeclaredConstructor(String.class);
			LogicBase lb = (LogicBase) (c).newInstance(logicData);
			lb.setClassName(className);
			lb.setMethodName(methodName);
			lb.setLogicData(logicData);
			lb.setExtendData(extendData);
			lb.setPage(page);
			lb.setPagesize(pagesize);
			lb.setRequest(request);
			
			String sRe = "";
			Object obj = lb.run();
			if(obj != null){
				sRe = obj.toString();
			}

			sb.append(sRe);
			out.print(sb.toString());
		} catch(Exception ex) {
			try {
				throw ex;
			} catch(Exception e1) {
				e1.printStackTrace();
			}
		}

	}

	public void destroy() {
	}

}
